package com.laowang.project.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.laowang.common.utils.SecurityUtils;
import com.laowang.project.system.domain.MyCyhActivity;
import com.laowang.project.system.domain.SysRole;
import com.laowang.project.system.mapper.SysRoleMapper;
import com.laowang.project.system.service.MyCyhActivityService;
import com.sun.jna.platform.win32.Winspool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MyCyhActivityCode;
import com.laowang.project.system.service.MyCyhActivityCodeService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 活动邀请码Controller
 *
 * @author laowang
 * @date 2023-07-03
 */
@Api(tags = "活动邀请码接口")
@RestController
@RequestMapping("/invitationCode")
public class MyCyhActivityCodeController extends BaseController {
    @Autowired
    private MyCyhActivityCodeService myCyhActivityCodeService;

    @Autowired
    private MyCyhActivityService myCyhActivityService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 查询活动邀请码列表
     */
    @ApiOperation(value = "查询活动邀请码列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:invitationCode:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCyhActivityCode myCyhActivityCode) {

        List<MyCyhActivity> myCyhActivities = myCyhActivityService.selectMyCyhActivityListWeb(null);
        Map<Integer, String> collect = myCyhActivities.stream().collect(Collectors.toMap(MyCyhActivity::getId, MyCyhActivity::getTitle));
        List<MyCyhActivityCode> list = new ArrayList<>();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        //不是管理员
        if (!userId.equals(1L)  ) {
            List<SysRole> sysRoles = sysRoleMapper.selectRolePermissionByUserId(userId);
            // 是 活动管理者
            Boolean b = sysRoles.stream().anyMatch(sysRole -> sysRole.getRoleKey().equals("activityManager"));
            if (b) {
                List<Integer> listIds = myCyhActivityService.selectActivityIdByCreateBy(userId);
                myCyhActivityCode.setListIds(listIds);
                list =  myCyhActivityCodeService.selectMyCyhActivityCodeByActivityId(myCyhActivityCode);
            }

        }else {
            startPage();
            list = myCyhActivityCodeService.selectMyCyhActivityCodeList(myCyhActivityCode);

        }
        for (MyCyhActivityCode cyhActivityCode : list) {
            if (collect.containsKey(cyhActivityCode.getActivityId())){
                cyhActivityCode.setActivityName(collect.get(cyhActivityCode.getActivityId()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出活动邀请码列表
     */
    @ApiOperation(value = "导出活动邀请码列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:invitationCode:export')")
    @Log(title = "活动邀请码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCyhActivityCode myCyhActivityCode) {
        List<MyCyhActivityCode> list = myCyhActivityCodeService.selectMyCyhActivityCodeList(myCyhActivityCode);
        ExcelUtil<MyCyhActivityCode> util = new ExcelUtil<MyCyhActivityCode>(MyCyhActivityCode.class);
        return util.exportExcel(list, "invitationCode");
    }

    /**
     * 获取活动邀请码详细信息
     */
    @ApiOperation(value = "获取活动邀请码详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:invitationCode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(myCyhActivityCodeService.selectMyCyhActivityCodeById(id));
    }

    /**
     * 新增活动邀请码
     */
    @ApiOperation(value = "新增活动邀请码",notes = "")
    @PreAuthorize("@ss.hasPermi('my:invitationCode:add')")
    @Log(title = "活动邀请码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCyhActivityCode myCyhActivityCode) {
        return toAjax(myCyhActivityCodeService.insertMyCyhActivityCode(myCyhActivityCode));
    }

    /**
     * 修改活动邀请码
     */
    @ApiOperation(value = "修改活动邀请码",notes = "")
    @PreAuthorize("@ss.hasPermi('my:invitationCode:edit')")
    @Log(title = "活动邀请码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCyhActivityCode myCyhActivityCode) {
        return toAjax(myCyhActivityCodeService.updateMyCyhActivityCode(myCyhActivityCode));
    }

    /**
     * 删除活动邀请码
     */
    @ApiOperation(value = "删除活动邀请码",notes = "")
    @PreAuthorize("@ss.hasPermi('my:invitationCode:remove')")
    @Log(title = "活动邀请码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(myCyhActivityCodeService.deleteMyCyhActivityCodeByIds(ids));
    }
}
